﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;

using Ivy.Common.Framework.Base;
using Ivy.Common.Framework.DataAccess;
using Ivy.SiteAdmin.Entity;

namespace Ivy.SiteAdmin.DataAccess
{
    public class ContentBinaryDataContentDao: IDaoInterface<ContentBinaryDataContent, ContentBinaryDataContentCriteria>
    {
        #region  Variable & constant
        #endregion  Variable & constant

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Properties
        #endregion Properties

        #region Public Methods
        #region IDaoInterface<ContentBinaryDataContent, ContentBinaryDataContentCriteria> Members
        public bool Insert(ContentBinaryDataContent ContentBinaryDataContentEntity)
        {
            bool IsOperationSuccessful = false;
            if (ContentBinaryDataContentEntity.EntityState == EntityState.New)
            {
                DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                CmdObj.CommandText = "Dasideen_ContentBinaryDataContent_Insert";
                CmdObj.CommandType = CommandType.StoredProcedure;

                TranslateContentBinaryDataContentFromEntityToDatabaseParameters(CmdObj, ContentBinaryDataContentEntity);

                DatabaseCommon.InsertStoredProcForParameterDatEntityBase(CmdObj, ContentBinaryDataContentEntity);


                DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfCreation", ParameterDirection.Output, DbType.DateTime, DBNull.Value);



                CmdObj.Connection = DbConn;
                DbConn.Open();
                CmdObj.Prepare();
                int _NoOfRecord = CmdObj.ExecuteNonQuery();
                if (_NoOfRecord == 1)
                {

                    ContentBinaryDataContentEntity.Sys_DateOfCreation = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfCreation"].Value);
                    ContentBinaryDataContentEntity.Sys_DateOfModification = ContentBinaryDataContentEntity.Sys_DateOfCreation;
                    ContentBinaryDataContentEntity.EntityState = EntityState.UnChanged;
                    IsOperationSuccessful = true;
                }
                CmdObj.Dispose();
                DbConn.Close();
            }
            else
            {

                throw new Exception("For Inserting new ContentBinaryDataContent entity data to database,[EntityState] attribute should be [New]");
            }

            return IsOperationSuccessful;
        }

        public bool Update(ContentBinaryDataContent ContentBinaryDataContentEntity)
        {
            bool IsOperationSuccessful = false;

            if (ContentBinaryDataContentEntity.EntityState == EntityState.Changed)
            {
                DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                CmdObj.CommandText = "Dasideen_ContentBinaryDataContent_Update";
                CmdObj.CommandType = CommandType.StoredProcedure;

                //DataAccessProviderUtility.AddParameter(CmdObj, "@ContentMetaDataKey", ParameterDirection.Input, DbType.Int32, ContentBinaryDataContentEntity.ContentMetaDataKey);

                TranslateContentBinaryDataContentFromEntityToDatabaseParameters(CmdObj, ContentBinaryDataContentEntity);
                DatabaseCommon.UpdateStoredProcForParameterDatEntityBase(CmdObj, ContentBinaryDataContentEntity);

                DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfModification", ParameterDirection.Output, DbType.DateTime, DBNull.Value);

                CmdObj.Connection = DbConn;
                DbConn.Open();
                CmdObj.Prepare();
                int _NoOfRecords = CmdObj.ExecuteNonQuery();
                if (_NoOfRecords > 0)
                {
                    ContentBinaryDataContentEntity.Sys_DateOfModification = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfModification"].Value);
                    ContentBinaryDataContentEntity.EntityState = EntityState.UnChanged;
                    IsOperationSuccessful = true;
                }

                CmdObj.Dispose();
                DbConn.Close();
            }
            else
            {
                throw new Exception("For Updating existing ContentBinaryDataContent entity data to database, [EntityState] attribute should be [Changed]");
            }

            return IsOperationSuccessful;
        }

        public bool Delete(ContentBinaryDataContent DataEntity)
        {
            throw new NotImplementedException();
        }

        public ContentBinaryDataContent GetByID(string ID)
        {
            throw new NotImplementedException();
        }

        public List<ContentBinaryDataContent> GetAll()
        {
            throw new NotImplementedException();
        }

        public List<ContentBinaryDataContent> GetByCriteria(ContentBinaryDataContentCriteria CriteriaEntity)
        {
            throw new NotImplementedException();
        }
        #endregion

        public ContentBinaryDataContent GetByContentMetaDataKey(int ContentMetaDataKey)
        {
            ContentBinaryDataContent _SelectedContentBinaryDataContent = null; //IMPORTANT NOTE:- Just declare the variable never create an instance, create instance only when required.

            DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
            DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
            CmdObj.CommandText = "Dasideen_ContentBinaryDataContent_GetByContentMetaDataKey";
            CmdObj.CommandType = CommandType.StoredProcedure;

            DataAccessProviderUtility.AddParameter(CmdObj, "@ContentMetaDataKey", ParameterDirection.Input, DbType.Int32, Convert.ToInt32(ContentMetaDataKey));


            CmdObj.Connection = DbConn;
            DbConn.Open();
            CmdObj.Prepare();
            DbDataReader _DataReader = CmdObj.ExecuteReader();

            if (_DataReader.HasRows)
            {
                if (_DataReader.Read())
                {
                    _SelectedContentBinaryDataContent = new ContentBinaryDataContent(); //IMPORTANT NOTE:- Create instance only when required.
                    TranslateContentBinaryDataContentFieldsFromReaderToEntity(_SelectedContentBinaryDataContent, _DataReader);
                }
            }
            //else
            //{
            //    throw new Exception("No ContentBinaryDataContent found");
            //}
            CmdObj.Dispose();
            DbConn.Close();

            return _SelectedContentBinaryDataContent;
        }
        #endregion Public Methods

        #region Events
        #endregion Events

        #region Private Methods
        private void TranslateContentBinaryDataContentFieldsFromReaderToEntity(ContentBinaryDataContent pContentBinaryDataContent, DbDataReader pDbDataReader)
        {

            pContentBinaryDataContent.ContentMetaDataKey = Convert.ToInt32(pDbDataReader["ContentMetaDataKey"]);
            pContentBinaryDataContent.ContentBinaryData = (Byte[])(pDbDataReader["ContentBinaryData"]);
            if (!pDbDataReader.IsDBNull(pDbDataReader.GetOrdinal("ContentThumbnailImage")))
            {
                pContentBinaryDataContent.ContentThumbnailImage = (Byte[])(pDbDataReader["ContentThumbnailImage"]);
            }
            else
            {
                pContentBinaryDataContent.ContentThumbnailImage = null;
            }
            pContentBinaryDataContent.ContentBinaryDataFileName = Convert.ToString(pDbDataReader["ContentBinaryDataFileName"]);
            pContentBinaryDataContent.ContentBinaryDataType = Convert.ToString(pDbDataReader["ContentBinaryDataType"]);
            pContentBinaryDataContent.ContentTotalDownloads = Convert.ToInt32(pDbDataReader["ContentTotalDownloads"]);

            DatabaseCommon.TranslateSystemFieldsFromReaderToEntity(pContentBinaryDataContent, pDbDataReader);

        }
        private void TranslateContentBinaryDataContentFromEntityToDatabaseParameters(DbCommand pDbCommand, ContentBinaryDataContent pContentBinaryDataContentEntity)
        {
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ContentMetaDataKey", ParameterDirection.Input, DbType.Int32, pContentBinaryDataContentEntity.ContentMetaDataKey);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ContentBinaryData", ParameterDirection.Input, DbType.Binary, pContentBinaryDataContentEntity.ContentBinaryData);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ContentThumbnailImage", ParameterDirection.Input, DbType.Binary, pContentBinaryDataContentEntity.ContentThumbnailImage);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ContentBinaryDataFileName", ParameterDirection.Input, DbType.String, pContentBinaryDataContentEntity.ContentBinaryDataFileName);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ContentBinaryDataType", ParameterDirection.Input, DbType.String, pContentBinaryDataContentEntity.ContentBinaryDataType);
            DataAccessProviderUtility.AddParameter(pDbCommand, "@ContentTotalDownloads", ParameterDirection.Input, DbType.Int32, pContentBinaryDataContentEntity.ContentTotalDownloads);
        }
        #endregion Private Methods
        
    }
}
